package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.commons.shiro.ShiroUser;
import net.microfigure.commons.utils.StringUtils;
import net.microfigure.model.FamilyInfo;
import net.microfigure.service.IFamilyInfoService;
import net.microfigure.commons.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-15
 */
@Controller
@RequestMapping("/familyInfo")
public class FamilyInfoController extends BaseController {

    @Autowired 
    private IFamilyInfoService familyInfoService;
    
    @GetMapping("/manager")
    public String manager() {
    	System.out.println("admin/familyInfo/familyInfoList");
        return "admin/familyInfo/familyInfoList";
    }
    
	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order,String familyNo) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		System.out.println("第一次传进来的=="+familyNo);
		if(familyNo==null) {
			familyNo="45042101010001";
		}
//		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
//		String name = user.getName();
//		System.out.println("当前登录的shiro用户名"+name);
		familyInfoService.selectDataGrid(pageInfo,familyNo);
		return pageInfo;
	}
    
	
	
	@PostMapping("/dataGridAll")
	@ResponseBody
	public Object dataGridAll(Integer page,Integer rows,String sort, String order,FamilyInfo familyInfo) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		System.out.println("/dataGridAll");
		//Map<String, Object> condition = new HashMap<String, Object>();
//		System.out.println("第一次传进来的=="+familyNo);
//		if(familyNo==null) {
//			familyNo="45042101010001";
//		}
//		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
//		String name = user.getName();
//		System.out.println("当前登录的shiro用户名"+name);
		System.out.println("dangqianfamilyInfo.getCardNo()"+familyInfo.getCardNo());
		if (familyInfo.getCardNo() != null){
			 //condition.put("cardNo", familyInfo.getCardNo());
			String cardNo = familyInfo.getCardNo();
		familyInfoService.selectDataGrid2(pageInfo,cardNo);
		}else {
		//pageInfo.setCondition(condition);
		familyInfoService.selectDataGrid(pageInfo);
		}
		return pageInfo;
	}
	
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage(Model model,String familyNo) {
    	FamilyInfo familyInfo = new FamilyInfo();
    	Integer familyInfoNo = familyInfoService.selectMaxfamilyInfoByfamilyNo(familyNo);
    	if(familyInfoNo==null) {
    		familyInfoNo=100000;
    	}
    	familyInfo.setFamilyInfoNo(familyInfoNo+1);
    	System.out.println("打印出查询出"+familyInfo.getFamilyInfoNo());
    	familyInfo.setFamilyNo(familyNo);
    	model.addAttribute("familyInfo", familyInfo);
    	
    	
        return "admin/familyInfo/familyInfoAdd";
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid FamilyInfo familyInfo) {

    	System.out.println("添加"+familyInfo.getCardNo());
        boolean b = familyInfoService.insert(familyInfo);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {

        boolean b = familyInfoService.deleteById(id);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        FamilyInfo familyInfo = familyInfoService.selectById(id);
        model.addAttribute("familyInfo", familyInfo);
        return "admin/familyInfo/familyInfoEdit";
    }
    
    /**
     * 编辑
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid FamilyInfo familyInfo) {

        boolean b = familyInfoService.updateById(familyInfo);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
